/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             ConfigHelper.cs
*        创建系统时间:       2012/11/2 19:07:08
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.Windows.Forms;
    using System.Xml;

    public class ConfigHelper
    {
        public static string LoginTo = "AppConfig";
        public static string SelectPath = "//appSettings/add";

        public static void GetConfig(BaseConfiguration configuration)
        {
            GetConfig(ConfigFielName, configuration);
        }

        public static void GetConfig(string fileName, BaseConfiguration configuration)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            configuration.CustomerCompanyName = GetValue(xmlDocument, "CustomerCompanyName");
            configuration.ConfigurationFrom = configuration.GetConfiguration(GetValue(xmlDocument, "ConfigurationFrom"));
            configuration.SoftName = GetValue(xmlDocument, "SoftName");
            configuration.SoftFullName = GetValue(xmlDocument, "SoftFullName");
            configuration.RunMode = configuration.GetRunMode(GetValue(xmlDocument, "RunMode"));
            configuration.RootMenuID = GetValue(xmlDocument, "RootMenuID");
            configuration.CurrentLanguage = GetValue(xmlDocument, "CurrentLanguage");
            configuration.Version = GetValue(xmlDocument, "Version");
            configuration.CommonWebService = GetValue(xmlDocument, "CommonWebService");
            configuration.WebService = GetValue(xmlDocument, "WebService");
            configuration.FileWebService = GetValue(xmlDocument, "FileWebService");
            configuration.WorkReportWebService = GetValue(xmlDocument, "WorkReportWebService");
            configuration.WebServiceUsername = GetValue(xmlDocument, "WebServiceUsername");
            configuration.WebServicePassword = GetValue(xmlDocument, "WebServicePassword");
            configuration.UseScopeAdmin = string.Compare(GetValue(xmlDocument, "UseScopeAdmin"), "TRUE", true, CultureInfo.CurrentCulture) == 0;
            configuration.UseLicensedPermission = string.Compare(GetValue(xmlDocument, "UseLicensedPermission"), "TRUE", true, CultureInfo.CurrentCulture) == 0;
            configuration.ServiceFactory = GetValue(xmlDocument, "ServiceFactory");
            configuration.ServicePath = GetValue(xmlDocument, "ServicePath");
            configuration.DbHelperClass = GetValue(xmlDocument, "DbHelperClass");
            configuration.DbHelperAssmely = GetValue(xmlDocument, "DbHelperAssmely");
            configuration.RecordLog = string.Compare(GetValue(xmlDocument, "RecordLog"), "TRUE", true, CultureInfo.CurrentCulture) == 0;
            configuration.LoginAssembly = GetValue(xmlDocument, "LoginAssembly");
            configuration.LoginForm = GetValue(xmlDocument, "LoginForm");
            configuration.MainForm = GetValue(xmlDocument, "MainForm");
            configuration.LoadUser = string.Compare(GetValue(xmlDocument, "LoadUser"), "TRUE", true, CultureInfo.CurrentCulture) == 0;
            configuration.AllowUserRegister = string.Compare(GetValue(xmlDocument, "AllowUserRegister"), "TRUE", true, CultureInfo.CurrentCulture) == 0;
            configuration.DbConnectionString = GetValue(xmlDocument, "DbConnectionString");
            configuration.DataBaseType = configuration.GetDataBaseType(GetValue(xmlDocument, "DataBaseType"));
            configuration.ServerName = GetValue(xmlDocument, "ServerName");
            configuration.DataBase = GetValue(xmlDocument, "DataBase");
            configuration.TrustLink = string.Compare(GetValue(xmlDocument, "TrustLink"), "YES", true, CultureInfo.CurrentCulture) == 0;
            configuration.Username = GetValue(xmlDocument, "Username");
            configuration.Password = GetValue(xmlDocument, "Password");
            configuration.RegisterKey = GetValue(xmlDocument, "RegisterKey");
        }

        public static Dictionary<string, string> GetLoginTo()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            XmlDocument document = new XmlDocument();
            document.Load(ConfigFielName);
            foreach (XmlNode node in document.SelectNodes(SelectPath))
            {
                if (node.Attributes["key"].Value.ToUpper().Equals("LoginTo".ToUpper()))
                {
                    dictionary.Add(node.Attributes["value"].Value, node.Attributes["dispaly"].Value);
                }
            }
            return dictionary;
        }

        public static string GetValue(string key)
        {
            return GetValue(ConfigFielName, SelectPath, key);
        }

        public static string GetValue(string fileName, string key)
        {
            return GetValue(fileName, SelectPath, key);
        }

        public static string GetValue(XmlDocument xmlDocument, string key)
        {
            return GetValue(xmlDocument, SelectPath, key);
        }

        public static string GetValue(string fileName, string selectPath, string key)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            return GetValue(xmlDocument, selectPath, key);
        }

        public static string GetValue(XmlDocument xmlDocument, string selectPath, string key)
        {
            string str = string.Empty;
            foreach (XmlNode node in xmlDocument.SelectNodes(selectPath))
            {
                if (node.Attributes["key"].Value.ToUpper().Equals(key.ToUpper()))
                {
                    return node.Attributes["value"].Value;
                }
            }
            return str;
        }

        public static string ConfigFielName
        {
            get
            {
                return System.AppDomain.CurrentDomain.BaseDirectory + @"\" + FielName;

            }
        }

        public static string FielName
        {
            get
            {
                return (LoginTo + ".xml");
            }
        }
    }
}